Skip to content

fix(logging): isolate per-run log handlers and prevent root logger pollution (#295) - #608

Merged
codelion merged 1 commit into
algorithmicsuperintelligence:mainfrom
im-apratap:fix-parallel-logging-isolation
Nov 23, 2026
Merged

codelion merged 1 commit into
algorithmicsuperintelligence:mainfrom
im-apratap:fix-parallel-logging-isolation

Conversation

@im-apratap

Copy link
Copy Markdown
Contributor

Summary

Fixes #295: Running multiple OpenEvolve instances concurrently or sequentially in the same Python process previously caused loggers to be clobbered because all handlers attached directly to the global root logger without run isolation filters.

Key Changes

  1. Package Logger Namespace Isolation:
    • Switched from attaching handlers to root_logger = logging.getLogger() to the package logger pkg_logger = logging.getLogger("openevolve") with propagate = False.
    • Avoids taking over the global/root logging namespace and prevents polluting host applications.
  2. Per-Run Context Isolation:
    • Added current_run_id ContextVar and RunIsolationFilter in openevolve/utils/logging_utils.py.
    • Each OpenEvolve instance generates a unique run_id and attaches a RunIsolationFilter(self.run_id) to its FileHandler and StreamHandler.
    • __init__ and run() scope logging to current_run_id, guaranteeing that concurrent asyncio runs only emit into their respective log files and console streams without duplicate emissions.
  3. Prefix Stacking Prevention:
    • Updated LoggerPrefixFilter to preserve record._openevolve_orig_name so applying filters with different prefixes across handlers does not stack prefixes (e.g., RUN2.RUN1.openevolve.controller).
  4. Handler Cleanup:
    • Added _cleanup_logging(), close(), and __del__() to OpenEvolve, automatically detaching handlers from openevolve and closing open file descriptors when run() finishes, on explicit close(), or upon garbage collection.
  5. CLI Override:
    • Updated openevolve/cli.py so --log-level updates both the root and openevolve package loggers.

Testing & Verification

  • Added comprehensive unit and regression tests in tests/test_parallel_logging_isolation.py:
    • TestRunIsolationFilterUnit: verifies matching, non-matching, and un-scoped filter behavior.
    • test_root_logger_is_not_polluted: verifies root logger handlers are not touched.
    • test_concurrent_runs_do_not_cross_write_logs: verifies two concurrent OpenEvolve runs executed via asyncio.gather only log to their respective log files.
    • test_console_output_is_not_duplicated_across_instances: verifies console output is emitted only once per message.
    • test_cleanup_removes_and_closes_handlers: verifies handlers are removed and file descriptors closed.
    • test_run_lifecycle_manages_logging_context_and_cleanup: verifies await controller.run() sets run context and cleans up on completion.
    • test_logger_prefix_does_not_stack: verifies prefixes do not nest across multiple filters.
  • Verified all existing tests in tests/test_logger_prefix.py and tests/test_coevolution.py continue to pass (31/31 passed).
  • Verified code passes black --check and isort --check.

…llution (algorithmicsuperintelligence#295)

- Move OpenEvolve logging from the global root logger to the 'openevolve' package logger with propagate=False, avoiding host application namespace pollution.
- Add current_run_id ContextVar and RunIsolationFilter to ensure handlers only emit records matching their active OpenEvolve run context during concurrent or sequential execution.
- Prevent LoggerPrefixFilter from stacking prefixes when records pass through multiple handlers with different prefixes.
- Implement _cleanup_logging(), close(), and __del__() to detach handlers and close file descriptors on completion or garbage collection.
- Add comprehensive unit and regression tests in tests/test_parallel_logging_isolation.py.
@codelion
codelion merged commit c690eba into algorithmicsuperintelligence:main Nov 23, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multiple Openevolve run calls in parallel result in clobbered loggers

2 participants